<?php
/**
 * Created by PhpStorm.
 * User: *!N.J
 * Date: 2021/8/28
 * Time: 10:58
 * QQ:1467572213
 */

namespace app\admin\controller;

use library\Controller;
use think\facade\Request;
use think\facade\Session;
use \app\admin\model\Order as Order_m;
use think\Db;

/**
 * 订单管理
 * Class Order
 * @package app\admin\controller
 */
class Order extends Controller
{
    /**
     * 派单列表
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '派单列表';
        $auto_grab = Order_m::index();
        $page = $auto_grab->render();
        $this->assign('pages', $page);
        $this->assign('list', $auto_grab);
        return $this->fetch();
    }

    /**
     * 在线派单
     * @auth true
     */
    public function push_order($uid)
    {
        $this->title = '在线派单';
        if (!$uid) $this->error('参数错误');
        $this->assign('uid',$uid);
        $keyword = input('get.keyword/s', '');
        $goods_list = Order_m::push_order($keyword);
        $page = $goods_list->render();
        $this->assign('pages', $page);
        $this->assign('list', $goods_list);
        return $this->fetch();
    }

    /**
     * 派出商品
     * @auth true
     */
    public function push_goods()
    {
        $this->title = '派出商品';
        if (Request::isPost()){
            $uid = Request::post('uid');
            $id = Request::post('id');
            if (!$uid || !$id) $this->error('参数错误');
            $push_goods = Order_m::push_goods($uid,$id);
            switch ($push_goods)
            {
                case 'success':
                    return ['code'=>1,'msg'=>'派单成功'];
                    break;
                case 'error':
                    return ['code'=>0,'msg'=>'派单失败,请联系技术!'];
                    break;
                case 'error_stop':
                    return ['code'=>0,'msg'=>'Stop Grab Error'];
                    break;
            }
        }
    }

    /**
     * 订单管理
     * @auth true
     */
    public function order_list()
    {
        $this->title = '订单列表';
        $keyword = Request::get('keyword');
        $list = Order_m::order_list($keyword);
        $page = $list->render();
        $this->assign('pages', $page);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 订单续期
     * @auth true
     */
    public function order_rollover()
    {
        $this->title = '订单续期';
        if (Request::isPost()) {
            $id = Request::post('id');
            $hour = Request::post('hour');
            $order_rollover = Order_m::order_rollover($id, $hour);
            switch ($order_rollover) {
                case 'success':
                    return ['code' => 1, 'msg' => '续期成功'];
                    break;
                case 'error':
                    return ['code' => 0, 'msg' => '系统错误'];
                    break;
            }
        }
    }

    /**
     * 订单解冻
     * @auth true
     */
    public function order_release()
    {
        $this->title = '订单解冻';
        if (Request::isPost()){
            $id = Request::post('id');
            $order_release = Order_m::order_release($id);
            switch ($order_release)
            {
                case 'success':
                    return ['code'=>1,'msg'=>'解冻成功'];
                    break;
                case 'error':
                    return ['code'=>0,'msg'=>'系统错误'];
                    break;
            }
        }
    }


    /**
     * 模式分组
     * @auth true
     */
    public function group_list()
    {
        $this->title = '模式分组';
        $keyword = Request::get('keyword');
        $list = Order_m::group_list($keyword);
        $page = $list->render();
        $this->assign('pages', $page);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加分组
     * @auth true
     */
    public function group_add()
    {
        $this->title = '添加分组';
        if (Request::isPost()) {
//            $info['aid'] = Request::post('aid');
            $info['name'] = Request::post('name');
            $pay = Request::post('pay');
            if ($pay) {
                $info['pay'] = implode(',', $pay);
            }
            $info['create_time'] = time();
            $add = Order_m::group_add($info);
            switch ($add) {
                case 'success':
                    $this->success('添加成功');
                    break;
                case 'error':
                    $this->error('添加失败');
                    break;
                case 'error_role':
                    $this->error('该用户还不是代理');
                    break;
                case 'not_user':
                    $this->error('ID输入错误,该用户不存在!');
                    break;
            }
        }
        return $this->fetch();
    }


    /**
     * 分组编辑
     * @auth true
     */
    public function group_edit()
    {
        $this->title = '分组编辑';
        $id = Request::get('id');
        $group_info = Order_m::group_info($id);
        $this->assign('group_info',$group_info);
        if (Request::isPost()){
            $group_id = Request::post('id');
            $info['name'] = Request::post('name');
            $pay = Request::post('pay');
            if ($pay){
                $info['pay'] = implode(',',$pay);
            }
            $info['update_time'] = time();
            $group_edit = Order_m::group_edit($group_id,$info);
            switch ($group_edit)
            {
                case 'success':
                    $this->success('修改成功');
                    break;
                case 'error':
                    $this->error('修改失败');
                    break;
            }
        }
        return $this->fetch();
    }

    /**
     * 分组用户
     * @auth true
     */
    public function group_user()
    {
        $this->title = '分组用户';
        if (Request::isPost()){
            $group_id = Request::post('group_id');
            $type = Request::post('type');
            $data = json_decode(Request::post('data'),true);
            foreach ($data as $k => $v){
                $add_user = Order_m::group_user($group_id,$type,$v['value']);
            }
            if ($add_user == 'success')
                return ['code'=>1,'msg'=>'修改成功'];
        }
        $id = Request::get('id');
        if ($id){
            $this->assign('id',$id);
            $group_info = Order_m::group_info($id);
            $this->assign('group_info',$group_info);
        }else{
            $this->error('参数错误11');
        }

        return $this->fetch();
    }

    /**
     * 分组信息
     * @auth true
     */
    public function get_pid()
    {
        $this->title = '分组信息';
        if (Request::isPost()){
            $group_id = Request::post('group_id');
            $get_pid_z = Order_m::get_pid_z();
            $z = [];
            foreach ($get_pid_z as $k => $v){
                $z[] = [
                    'value'=>$v['id'],
                    'title'=>$v['username']
                ];
            }
            $get_pid_y = Order_m::get_pid_y($group_id);

            $y = [];
            foreach ($get_pid_y as $k => $v){
                $y[] = [
                    $v['id'],
                ];
            }
            return ['z'=>$z,'y'=>$y];
        }
    }

    /**
     * 编辑模板
     * @auth true
     */
    public function group_mode()
    {
        $this->title = '编辑模板';
        $level_info = Order_m::level_info();
        $this->assign('level_info',$level_info);
        $my_group = Order_m::group_select();
        $this->assign('my_group',$my_group);
        $pop_pic_list = Order_m::pop_pic_list($keyword = null);
        $this->assign('pop_pic_list',$pop_pic_list);
        $id = Request::get('id');
        if ($id){
            $group_info = Order_m::group_info($id);
            $this->assign('group_info',$group_info);
            $group_mode = Order_m::group_mode($id);
            if ($group_mode){
                $this->assign('group_mode',$group_mode);
            }
        }else{
            $this->error('参数错误');
        }
        return $this->fetch();
    }

    /**
     * 保存模板
     * @auth true
     */
    public function mode_save()
    {
        $this->title = '保存模板';
        $level_info = Order_m::level_info();
        $this->assign('level_info',$level_info);
        $my_group = Order_m::group_select();
        $this->assign('my_group',$my_group);
        if (Request::isPost()){
            $mode['aid'] = Request::post('aid');
            $mode['group_id'] = Request::post('group_id');
            $mode['grab_type'] = Request::post('grab_type');
            $mode['is_windows'] = Request::post('is_windows');
            if (!$mode['is_windows']) $mode['is_windows'] = 0;
            $mode['windows_img'] = Request::post('windows_img');
            $mode['is_level'] = Request::post('is_level');
            if (!$mode['is_level']) $mode['is_level'] = 0;
            $mode['level'] = Request::post('level');
            $mode['is_group'] = Request::post('is_group');
            if (!$mode['is_group']) $mode['is_group'] = 0;
            $mode['group'] = Request::post('group');
            $mode['odd_num'] = Request::post('odd_num');
            $mode['pay_mode'] = Request::post('pay_mode');
            $mode['pay_value'] = Request::post('pay_value');
            $mode['rand_num'] = Request::post('rand_num');
            $mode['rand_bili'] = Request::post('rand_bili');
            $mode['addition'] = Request::post('addition');
            $mode['create_time'] = time();
            $mode_save = Order_m::mode_save($mode);
            switch ($mode_save)
            {
                case 'success':
                    return ['code'=>1,'msg'=>'保存成功'];
                    break;
                case 'error':
                    return ['code'=>0,'msg'=>'保存失败'];
                    break;
            }
        }
    }

    /**
     * 删除模板
     * @auth true
     */
    public function del_mode()
    {
        $this->title = '删除模板';
        if (Request::isPost()){
            $id = Request::post('id');
            $del_mode = Order_m::del_mode($id);
            switch ($del_mode)
            {
                case 'success':
                    return ['code'=>1,'msg'=>'删除成功'];
                    break;
                case 'error':
                    return ['code'=>0,'msg'=>'删除失败'];
                    break;
            }
        }
    }

    /**
     * 删除分组
     * @auth true
     */
    public function group_del()
    {
        $this->title = '删除分组';
        if (Request::isPost()){
            $id = Request::post('id');
            $group_del = Order_m::group_del($id);
            switch ($group_del)
            {
                case 'success':
                    return ['code'=>1,'msg'=>'删除成功'];
                    break;
                case 'error':
                    return ['code'=>0,'msg'=>'删除失败'];
                    break;
                case 'error_ues':
                    return ['code'=>0,'msg'=>'还有用户在该分组,请清除后再删除.'];
                    break;
            }
        }
    }

    /**
     * 弹窗图片
     * @auth true
     */
    public function pop_pic_list()
    {
        $this->title = '弹窗图片';
        $keyword = Request::get('keyword');
        $list = Order_m::pop_pic_list($keyword);
        $page = $list->render();
        $this->assign('pages', $page);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加弹窗
     * @auth true
     */
    public function pop_pic_add()
    {
        if (Request::isPost()){
            $info['name'] = Request::post('name');
            $info['pic'] = Request::post('pic');
            $info['create_time'] = time();
            $add = Order_m::pop_pic_add($info);
            switch ($add)
            {
                case 'success':
                    $this->success('添加成功');
                    break;
                case 'error':
                    $this->error('添加失败');
                    break;
            }
        }
        return $this->fetch();
    }
    
    /**
     * 添加弹窗
     * @auth true
     */
    public function pop_pic_edit($id)
    {
        if (Request::isPost()){
            // echo 1;die;
            $info['name'] = Request::post('name');
            $info['pic'] = Request::post('pic');
            $info['create_time'] = time();
            $add = Db::name('fd_pop')->where('id',$id)->update($info);
            switch ($add)
            {
                case 'success':
                    $this->success('添加成功');
                    break;
                case 'error':
                    $this->error('添加失败');
                    break;
            }
        }
        // $id = Request::get('id');
        // echo $id;die;
        $res = Db::name('fd_pop')->where('id',$id)->find();
        // var_dump($res);die;
        $this->assign('res', $res);
        
        
        return $this->fetch();
    }

    /**
     * 删除弹窗
     * @auth true
     */
    public function pop_pic_del()
    {
        if (Request::isPost()){
            $id = Request::post('id');
            $res = Order_m::pop_pic_del($id);
            switch ($res)
            {
                case 'success':
                    $this->success('删除成功');
                    break;
                case 'error':
                    $this->error('删除失败');
                    break;
            }
        }
    }

    /**
     * 新增订单
     * @auth true
     */
    public function order_add()
    {
        $this->title = '新增订单';
        $oid = Request::get('oid');
        if ($oid){
            $goods_name = trim(Request::get('goods_name'));
            $money_min = trim(Request::get('money_min'));
            $money_max = trim(Request::get('money_max'));
            $goods_list = Order_m::get_goods_list($goods_name,$money_min,$money_max);
            $page = $goods_list->render();
            $this->assign('pages', $page);
            $this->assign('list', $goods_list);
            $this->assign('oid',$oid);
            return $this->fetch();
        }else{
            $this->error('参数错误');
        }
        return $this->fetch();
    }
    
    public function order_add_run()
    {
        if (Request::isPost()) {
            $oid = Request::post('oid');
            $goods_id = Request::post('goods_id');
            $goods_id = explode(',',$goods_id);
            $add = Order_m::order_add_run($oid,$goods_id);
            if ($add == 'success'){
                return ['code'=>1,'msg'=>'添加成功'];
            }else{
                return ['code'=>0,'msg'=>'添加失败'];
            }
        }
    }

    /**
     * 订单删除
     * @auth true
     */
    public function order_del()
    {
        if (Request::isPost()){
            $id = Request::post('id');
            $del = Order_m::order_del($id);
            if ($id){
                switch ($del)
                {
                    case 'success':
                        return ['code'=>1,'msg'=>'删除成功'];
                        break;
                    case 'error':
                        return ['code'=>0,'msg'=>'删除失败'];
                        break;
                }
            }else{
                return ['code'=>0,'msg'=>'参数错误'];
            }
        }
    }

    /**
     * 订单冻结
     * @auth true
     */
    public function order_frozen()
    {
        if (Request::isPost()){
            $id = Request::post('id');
            if ($id){
                $frozen = Order_m::order_frozen($id);
                switch ($frozen)
                {
                    case 'success':
                        return ['code'=>1,'msg'=>'冻结成功'];
                        break;
                    case 'error':
                        return ['code'=>0,'msg'=>'冻结失败'];
                        break;
                }
            }else{
                return ['code'=>0,'msg'=>'参数错误'];
            }
        }
    }

}